package com.atan.xiaohongshu.auth.sms;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.atan.framework.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AliyunSmsSendHelper {

    @Autowired
    private com.aliyun.dysmsapi20170525.Client client;

    public boolean sendSmsMessage(String phone, String signName, String templateCode, String templateParam)  {
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phone)
                .setTemplateParam(templateParam);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            log.info("==> 开始短信发送, phone: {}, signName: {}, templateCode: {}, templateParam: {}", phone, signName, templateCode, templateParam);
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            // 检查响应中的 code 是否为 "OK"
            if ("OK".equals(sendSmsResponse.getBody().getCode())) {
                log.info("==> 短信发送成功, response: {}", JsonUtils.toJsonString(sendSmsResponse));
                return true;
            } else {
                log.error("==> 短信发送失败, code: {}, message: {}", sendSmsResponse.getBody().getCode(), sendSmsResponse.getBody().getMessage());
                return false;
            }
        } catch (Exception error) {
            log.error("==> 短信发送错误: ", error);
            return false;
        }
    }
}

